//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package cn.autumnorange.app.common.consumer.security;

import cn.autumnorange.app.common.rpc.exception.JsonException;
import lombok.Getter;
import lombok.Setter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.error.AbstractOAuth2SecurityExceptionHandler;
import org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Getter
@Setter
public class JsonOAuth2AuthenticationEntryPoint extends AbstractOAuth2SecurityExceptionHandler
    implements AuthenticationEntryPoint {
  private String typeName = "Bearer";
  private String realmName = "oauth";
  private WebResponseExceptionTranslator exceptionTranslator =
      new DefaultWebResponseExceptionTranslator();
  private HandlerExceptionResolver handlerExceptionResolver = new DefaultHandlerExceptionResolver();

  private JsonException jsonExceptionImpl;

  public JsonOAuth2AuthenticationEntryPoint(JsonException jsonExceptionImpl) {
    this.jsonExceptionImpl = jsonExceptionImpl;
  }

  public void setRealmName(String realmName) {
    this.realmName = realmName;
  }

  public void setTypeName(String typeName) {
    this.typeName = typeName;
  }

  public void commence(
      HttpServletRequest request,
      HttpServletResponse response,
      AuthenticationException authException)
      throws IOException, ServletException {
    this.jsonDoHandle(request, response, authException);
  }

  protected void jsonDoHandle(
      HttpServletRequest request, HttpServletResponse response, Exception authException)
      throws IOException, ServletException {
    try {
      ResponseEntity<OAuth2Exception> result = this.exceptionTranslator.translate(authException);
      result = this.enhanceResponse(result, authException);
      //            response.getWriter().println(result);
      jsonExceptionImpl.jsonException(
          request, response, authException, result.getStatusCode().value());
    } catch (ServletException var5) {
      if (this.handlerExceptionResolver.resolveException(request, response, this, var5) == null) {
        throw var5;
      }
    } catch (IOException var6) {
      throw var6;
    } catch (RuntimeException var7) {
      throw var7;
    } catch (Exception var8) {
      throw new RuntimeException(var8);
    }
  }

  protected ResponseEntity<OAuth2Exception> enhanceResponse(
      ResponseEntity<OAuth2Exception> response, Exception exception) {
    HttpHeaders headers = response.getHeaders();
    String existing = null;
    if (headers.containsKey("WWW-Authenticate")) {
      existing = this.extractTypePrefix(headers.getFirst("WWW-Authenticate"));
    }

    StringBuilder builder = new StringBuilder();
    builder.append(this.typeName + " ");
    builder.append("realm=\"" + this.realmName + "\"");
    if (existing != null) {
      builder.append(", " + existing);
    }

    HttpHeaders update = new HttpHeaders();
    update.putAll(response.getHeaders());
    update.set("WWW-Authenticate", builder.toString());
    return new ResponseEntity(response.getBody(), update, response.getStatusCode());
  }

  private String extractTypePrefix(String header) {
    String existing = header;
    String[] tokens = header.split(" +");
    if (tokens.length > 1 && !tokens[0].endsWith(",")) {
      existing = StringUtils.arrayToDelimitedString(tokens, " ").substring(header.indexOf(" ") + 1);
    }

    return existing;
  }
}
